<?php
class ModelSettings
{
	public function construct()
	{
		
	}
	
	public function loadBackupFiles()
	{
		$file_location = DOCROOT . "library/data_backup";
		$thelist = "Choose backup file you suppose to import <br /><br />";
		if ($handle = opendir($file_location)) {
			while (false !== ($file = readdir($handle)))
			{
				if ($file != "." && $file != "..")
				{
			    	$thelist .= '<img src="images/shared/bkpfile.png" width="45" height="56" /><a href="javascript: void(0)" onclick="importProcess(\'' . $file . '\')">'.$file.'</a><br />';
			    }
			}
			closedir($handle);
		}
		
		echo $thelist;
	}
	
	public function processBackup($filename)
	{
		$file_location = DOCROOT . "library/data_backup/" . $filename;			
		
		$fp = fopen ($file_location, "r"); 
		while (!feof ($fp)) { 
			$content_arr = fgets( $fp, 4096 ); 
			$content = explode(",", $content_arr);
			$itemcode = $content[0];
			$itemname = $content[1];
			$quantity = $content[2];
			$price = $content[3];

			$sql = "SELECT item_code 
					FROM tbl_products
					WHERE item_code = '" . $itemcode . "' ";
			$rs = DB::rowCount($sql);
			
			if($rs == 0)
			{
				$sql2 = "INSERT INTO tbl_products(item_code, product_title, quantity, product_price_lkr) VALUES('" . $itemcode . "', '" . $itemname . "', '" . $quantity . "', '" . $price . "')";
				//$import_items = DB::exec($sql2);	
			} else {
				$sql2 = "UPDATE tbl_products SET item_code = '" . $itemcode . "', product_title = '" . $itemname . "', quantity = '" . $quantity . "', product_price_lkr = '" . $price . "' ";
				//$import_items = DB::exec($sql2);
			}
			$import_items = DB::exec($sql2);
			
			$sql3 = "SELECT COUNT(item_code) AS ITEMCOUNT
					 FROM tbl_products";
			$itemcount_arr = DB::fetchRow($sql3);
			
			$item_count = $itemcount_arr['ITEMCOUNT'];
		} 
		
		if($import_items) 
		{
			echo "<span class='success' style='font-weight: bold;'>Item import process is successfully completed.<br />Total " . $item_count . " of rows updated.</span>";	
		}
	}
	
	public function importRateBkFile()
	{
		$sql = "SELECT count(*) AS ROWCOUNT
					FROM tbl_rates";
		$rs = DB::fetchRow($sql);
		$rowcount = $rs['ROWCOUNT'];
		
		if($rowcount > 0)
		{
			echo "<span class='error'>Are you sure you want to update the currency table? if you choose 'YES' existing data will be deleted. <br />Ready to continue? <input type='button' name='yes' id='yes' value='YES' onclick='continueInportRates()' />&nbsp;<input type='button' name='no' id='no' value='NO' onclick='abortratesImportProcess();' /></span><br /><div id='msg_box2' style='display: none;'></div>";
		}	
	}
	
	public function importRates2()
	{
		
		$sql1 = "DELETE FROM tbl_rates";
		DB::exec($sql1);
		
		$file_location = DOCROOT . "library/data_backup/rates.bkp";
		
		$fp = fopen ($file_location, "r"); 
		while (!feof ($fp)) { 
			$content_arr = fgets($fp, 4096); 
			
			$content = explode(",", $content_arr);
			$rate_date = $content[0];
			$currency = $content[1];
			$rate = $content[2];	
			
			$sql2 = "INSERT INTO tbl_rates(rate_date, currency, rate) VALUES('" . $rate_date . "', '" . $currency . "', '" . $rate . "');";			
			$import_items = DB::exec($sql2);
		} 
		
		if($import_items) 
		{
			echo "<span class='success' style='font-weight: bold;'>Item import process is successfully completed.</span>";	
		}		
	}
}
?>